﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Uco.Infrastructure;

namespace Uco.Models
{
    public class BaseController : Controller
    {

    }

    public class BasePageController : BaseController
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (CurrentPage != null)
            {
                ViewBag.Title = CurrentPage.SeoTitle != null && CurrentPage.SeoTitle != "" ? CurrentPage.SeoTitle : CurrentPage.Title;
                ViewBag.H1 = CurrentPage.SeoH1 != null && CurrentPage.SeoH1 != "" ? CurrentPage.SeoH1 : CurrentPage.Title;
                ViewBag.Keywords = CurrentPage.SeoKywords;
                ViewBag.Description = CurrentPage.SeoDescription;
                ViewBag.ID = CurrentPage.ID;
				ViewBag.Url = CurrentPage.Url;
            }
            else
            {
                throw new HttpException(404, "NotFound");
            }

            base.OnActionExecuting(filterContext);
        }

        private AbstractPage _CurrentPage = null;
        public AbstractPage CurrentPage
        {
            get
            {
                if (_CurrentPage != null) return _CurrentPage;
                
                if (RouteData.Values["name"] == null) return null;
                string Url = (string)RouteData.Values["name"];

                _CurrentPage = SF.LoadPage(Url);
                return _CurrentPage;
            }
        }
    }

    public class BaseAdminController : BaseController
    {
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            if (requestContext.HttpContext.Session["LangSelectList"] != null)
            {
                string LangSelectList = requestContext.HttpContext.Session["LangSelectList"] as string;
                if (!string.IsNullOrEmpty(LangSelectList))
                {
                    System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(LangSelectList);
                    System.Threading.Thread.CurrentThread.CurrentCulture = culture;
                    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
                }
            }
        }
    }
}